package a07_二叉树;

/**
 * <p>
 * a32_将有序数组转换为二叉搜索树复习1
 * </p>
 *
 * @author flyduck
 * @since 2024/12/27
 */
public class a32_将有序数组转换为二叉搜索树复习1 {
    public TreeNode sortedArrayToBST(int[] nums) {
        return traversal(nums, 0, nums.length -1);
    }

    public TreeNode traversal(int[] nums,int leftIdx,int rightIdx){
        if (leftIdx > rightIdx) {
            return null;
        }

        int middleIdx = leftIdx + (rightIdx - leftIdx)/2;
        TreeNode rootNode = new TreeNode(nums[middleIdx]);

        rootNode.left = traversal(nums, leftIdx, middleIdx - 1);
        rootNode.right = traversal(nums, middleIdx + 1, rightIdx);
        return rootNode;
    }
}
